package edu.cmu.javadb;

import java.beans.*;

import edu.cmu.javadb.exceptions.NoAttributeException;

public abstract class BaseRecord {
	protected PropertyChangeSupport propertySupport = new PropertyChangeSupport(
			this);

	protected void addPropertyChangeListener(PropertyChangeListener listener) {
		propertySupport.addPropertyChangeListener(listener);
	}

	protected void removePropertyChangeListener(PropertyChangeListener listener) {
		propertySupport.removePropertyChangeListener(listener);
	}
	
	protected void changeAttribute(Object newVal) {
		StackTraceElement[] traceElements = Thread.currentThread().getStackTrace();
		String attName = traceElements[2].getMethodName().substring(3);
		//listener should automatically know the old value
		try {
			Attribute att = RecordInfo.getRecordInfo(this.getClass()).getAttribute(attName);
			//Object oldVal = att.getValue(this);
			propertySupport.firePropertyChange(attName, null, newVal);
			att.setFieldDirectly(this, newVal);
		} catch (NoAttributeException e) {
			e.printStackTrace();
		}	
	}

}
